package com.xuecheng.manage_course.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.manage_course.entity.CourseMarket;
import com.xuecheng.manage_course.entity.CoursePic;
import com.xuecheng.manage_course.entity.CoursePub;
import com.xuecheng.manage_course.mapper.CourseMarketMapper;
import com.xuecheng.manage_course.mapper.CoursePubMapper;
import com.xuecheng.manage_course.mapper.TeachplanMapper;
import com.xuecheng.manage_course.service.CoursePubService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-14
 */
@Service
public class CoursePubServiceImpl extends ServiceImpl<CoursePubMapper, CoursePub> implements CoursePubService {

    @Autowired
    CoursePicServiceImpl coursePicService;

    @Autowired
    CourseMarketMapper courseMarketMapper;

    @Autowired
    TeachplanMapper teachplanMapper;

    //将coursePub对象保存到数据库
    public CoursePub saveCoursePub(String id, CoursePub coursePub){
        //根据课程id查询coursePub
        CoursePub coursePubNew = baseMapper.selectById(id);
        if(coursePubNew!=null){

        }else{
            coursePubNew = new CoursePub();
        }

        //将coursePub对象中的信息保存到coursePubNew中
        BeanUtils.copyProperties(coursePub,coursePubNew);
        coursePubNew.setId(id);
        //时间戳,给logstach使用
        coursePubNew.setTimestamp(new Date());
        //发布时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String date = simpleDateFormat.format(new Date());
        coursePubNew.setPubTime(date);
        baseMapper.insert(coursePubNew);
        return coursePubNew;
    }
    //创建coursePub对象
    public CoursePub createCoursePub(String id){
        CoursePub coursePub = new CoursePub();
        //根据课程id查询course_base
        CoursePub coursePub1 = baseMapper.selectById(id);
        if(coursePub1 != null){

            //将courseBase属性拷贝到CoursePub中
            BeanUtils.copyProperties(coursePub1,coursePub);
        }

        //查询课程图片
        CoursePic coursePic1 = coursePicService.findCoursePic(id);
        if(coursePic1 != null){
            BeanUtils.copyProperties(coursePic1, coursePub);
        }

        //课程营销信息
        CourseMarket courseMarket1 = courseMarketMapper.selectById(id);
        if(courseMarket1 != null){
            BeanUtils.copyProperties(courseMarket1, coursePub);
        }

        //课程计划信息
        TeachplanNode teachplanNode = teachplanMapper.selectList1(id);
        String jsonString = JSON.toJSONString(teachplanNode);
        //将课程计划信息json串保存到 course_pub中
        coursePub.setTeachplan(jsonString);
        return coursePub;

    }

}
